 
import { ABaseDecision } from "GameLoop/DecisionMaking/BaseDecision";
import { getRoleConfig } from "utils/constants/bodyconstant";
import { priority } from "utils/constants/definetypes";
import { BodyUtils } from "utils/creep/bodyutils";
import { Roles } from "utils/creep/setups";
import { PositionHelper } from "utils/position/PositionHelper";
import { UtilsHelper } from "utils/position/UtilsHelper";
import { TaskHelper } from "utils/task/TaskHelper";
import { generateUniqueId } from "utils/utils";

/**
 * 外矿房的采集任务
 */
export class HarvestOutMineDecision<  T extends "creep"="creep"> extends ABaseDecision<T>
{
    // 找不到任务。就新增
    public isExpired(event: KeepEvent, region: string): boolean 
    {
        return true;
        // const room = Game.rooms[event.region];
        // const harvesttask = room.getTaskByType("creep").filter(e=>e.data.type == "harvest");
        
        // return harvesttask.length==0;
    }
    public makeTask(event: KeepEvent, region: string): IDecisionCode<T>|false
    {
        
        const room = Game.rooms[event.region] as Required<Room>;
        // this.updateReserveTask(room);
        const  Mine = room.getEntitys("OutMineEntity");
    
        // 俩矿工俩采集工
        const tasks=[];
        const bodyconfig =  getRoleConfig("mine",room.controller.level);
        const carrybodyconfig =  getRoleConfig("porter",room.lordRoom().controller.level);
        for(const e of Mine)
        {
            if(e.getNodeMemory().state)
            {
               // global.Log.Debug(`Mine状态更新${e.GUID()}`,`删除任务${e.GUID()}`);
                if(e.RoomType == 6 || UtilsHelper.isCenterRoom(e.roomName)==2)
                {
                    // 删除9房的战斗任务
                    room.deleteTaskById((t:ITaskDeclare[T])=>t.data.room == e.roomName,"team");
                }
                
                room.deleteTaskById(e.GUID(),"creep")
                continue;
            }
            if(e.RoomType == 6 && UtilsHelper.isCenterRoom(e.roomName)!=2)
            {
                this.WorldGetter.registerNotify(event.region,{region:event.region,room:e.roomName,type:"DefendCenterRoom"})
                
            }
            //global.Log.Debug(`添加采集任务${e.GUID()}`,`${e.GUID()}_`)
            // 采集工数量
            const work_amont =Math.min( BodyUtils.getCreepCount(bodyconfig.body,WORK,e.getWorkBodyNum()),e.getNodeMemory().max);
            const carry_amount =  BodyUtils.getCreepCount(carrybodyconfig.body,CARRY,e.getCarryBodyNum()* (UtilsHelper.isCenterRoom(event.room)?1: room.memory.stats.porterRadio));
            const curTask = room.findTaskById<ITask<ICreepTaskData<"harvest">>>( e.GUID(),"creep");
            if(curTask)
            {
                curTask.data.arg = {
                    entityId:e.GUID(),
                }
                if(room.controller.level>3)
                {
                    curTask.data.required = [
                        [ ["mine"], 1,work_amont],
                        [ ["porter_w"],1,1],
                        [ ["porter"],carry_amount ==1?0:1,carry_amount-1],
                        
                    ]
                }
                else 
                {
                    curTask.data.required = [
                        [ ["mine"], 1,work_amont],
                        [ ["porter"],1,carry_amount]
                    ]
                }
                
            }
            else
            {
                const task:ITaskDeclare["creep"]={
                    id:  e.GUID(),
                    priority:priority.C,
                    ttl:-1,
                    type:"creep",
                    data:{
                        type:"harvest",
                        arg:{
                            entityId:e.GUID(),
                        },
                        creeps:[],
                        body:{},        // 当前统计的body数量
                        income:1500/300 *1500 * (work_amont/6.0), // 1500T内的收入
                        room:e.roomName,
                        cost:TaskHelper.getTaskPathCost(room.name,e.roomName),
                        required:[
                            [ ["mine"],1,work_amont],
                            [ ["porter"],1,carry_amount]
                        ]
                    },
                    shard:e.getAroundPos().length,
                }
                tasks.push(task);
            }
        }
        if(tasks.length>0)
        {
            const code:IDecisionCode<T>={
                region:event.region,
                task:tasks,
                isunique:false,
            }
            return code
        }
        return false
    }
    

    private updateReserveTask(room:Required<Room>)
    {
       
        const reservetasks = room.getTaskByType("creep",e=>e.data.type == "claim" && (e.data.arg as IClaimTask).action == "reserve");
        room.log("info","预定控制器",JSON.stringify(reservetasks.map(e=>e.id)));
        if(room.controller.level>=5)
        {
            // 查找更新
            reservetasks.forEach(e=>e.data.required.find(e=>e[0][0] == "claimer")![1]=1)
        }
        else 
        {
            reservetasks.forEach(e=>e.data.required.find(e=>e[0][0] == "claimer")![1]=2)
        }
    }
    
}